/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-07-07
 * Time: 10:21
 */
public class SingleLinkedList {
    static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public int size;
    ListNode head;

    public SingleLinkedList() {
        this.size = 0;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
        size++;
    }
    //尾插法
    public void addLast(int data){
        ListNode last = head;
        while(last.next != null)last = last.next;
        ListNode node = new ListNode(data);
        last.next = node;
        size++;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > size()){
            throw new IndexNotLegalException("下标不合法！");
        }
        if(index == 0){
            addFirst(data);
        }
        else if (index == size()) {
            addLast(data);
        }
        else{
            ListNode pre = head;
            while(--index != 0){
                pre = pre.next;
            }
            ListNode node = new ListNode(data);
            node.next = pre.next;
            pre.next = node;
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode node = head;
        while(node != null){
            if(node.val == key)return true;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head.val == key){
            head = head.next;
        }
        ListNode node = head.next;
        ListNode pre = head;
        while(node != null){
            if(node.val == key){
                pre.next = node.next;
                size--;
                return;
            }
            pre = pre.next;
            node = node.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null)return;
        while(head.val == key){
            head = head.next;
            if(head == null)return;
        }
        ListNode node = head.next;
        ListNode pre = head;
        while(node != null){
            if(node.val == key){
                pre.next = node.next;
                node = pre.next;
                size--;
                continue;
            }
            pre = pre.next;
            node = node.next;
        }
    }
    //得到单链表的长度
    public int size(){
        return size;
    }
    public void display(){
        ListNode node = head;
        while(node != null){
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }
    public void clear(){
        head = null;
        size = 0;
    }

    public static void main(String[] args) {
        SingleLinkedList list = new SingleLinkedList();
        list.addFirst(10);
        list.addFirst(20);
        list.addFirst(30);
        list.addLast(40);
        list.addLast(40);
        list.addLast(40);
        list.addLast(40);
        list.display();
        list.remove(40);
        list.display();
        list.removeAllKey(40);
        list.display();
    }
}
